package se.liu.oschi129.world.model;

import java.awt.event.KeyEvent;

/**
 * This class gives WorldObjects an abstract way to access keyboard inputs.
 * 
 * @author oschi129
 */
public class WorldInput {

	public enum ActionType {PRESSED, RELEASED, TYPED};
	
	private ActionType type;
	private int key;
	
	private final int JUMP_KEY = KeyEvent.VK_SPACE;
	private final int CROUCH_KEY = KeyEvent.VK_DOWN;
	private final int RUN_KEY = KeyEvent.VK_SHIFT;
	private final int LEFT_KEY = KeyEvent.VK_LEFT;
	private final int RIGHT_KEY = KeyEvent.VK_RIGHT;
	private final int CLIMB_KEY = KeyEvent.VK_UP;
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - - Constructors - - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	public WorldInput(KeyEvent event, ActionType type) {
		key = event.getKeyCode();
		this.type = type;
	}
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - Public methods - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	/** Returns true if the jump button was pressed */
	public boolean jumpDown() {
		return checkInput(JUMP_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the jump button was released */
	public boolean jumpUp() {
		return checkInput(JUMP_KEY,ActionType.RELEASED);
	}
	
	/** Returns true if the crouch button was pressed */
	public boolean crouchDown() {
		return checkInput(CROUCH_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the crouch button was released */
	public boolean crouchUp() {
		return checkInput(CROUCH_KEY,ActionType.RELEASED);
	}
	
	/** Returns true if the climb button was pressed */
	public boolean climbDown() {
		return checkInput(CLIMB_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the climb button was released */
	public boolean climbUp() {
		return checkInput(CLIMB_KEY,ActionType.RELEASED);
	}
	
	/** Returns true if the run button was pressed */
	public boolean runDown() {
		return checkInput(RUN_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the run button was released */
	public boolean runUp() {
		return checkInput(RUN_KEY,ActionType.RELEASED);
	}
	
	/** Returns true if the move left button was pressed */
	public boolean leftDown() {
		return checkInput(LEFT_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the move left button was released */
	public boolean leftUp() {
		return checkInput(LEFT_KEY,ActionType.RELEASED);
	}
	
	/** Returns true if the move right button was pressed */
	public boolean rightDown() {
		return checkInput(RIGHT_KEY,ActionType.PRESSED);
	}
	
	/** Returns true if the move right button was released */
	public boolean rightUp() {
		return checkInput(RIGHT_KEY,ActionType.RELEASED);
	}
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - Private methods - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	/** Returns true if a KeyEvent has occurred with a specified key and action type */
	private boolean checkInput(int key, ActionType type) {
		return (this.key == key && this.type == type);
	}
	
}
